package libs.yzx.zaza.util;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;

/**
 * Created by Administrator on 2016/10/19
 */
@Deprecated //没完成..有问题
public class ActiveProgressView extends View{

    public ActiveProgressView(Context context) {
        super(context);init();
    }
    public ActiveProgressView(Context context, AttributeSet attrs) {
        super(context, attrs);init();
    }
    public ActiveProgressView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);init();
    }
    private void init(){
        addOnAttachStateChangeListener(onAttachStateChangeListener);
    }
    private OnAttachStateChangeListener onAttachStateChangeListener = new OnAttachStateChangeListener() {
        public void onViewAttachedToWindow(View v) {
        }
        public void onViewDetachedFromWindow(View v) {
            cancelAnim();
            progress = 0;
            post(invalidateRun);
        }
    };
    private ValueAnimator anim;
    private int animProgress;
    private void cancelAnim(){
        if(anim != null){
            anim.cancel();
            anim=null;
        }
    }
    private void startAnim(){
        if(anim != null) return ;
        anim = ValueAnimator.ofInt(0, (int)progress+animRectWidth).setDuration(1300);
        anim.setRepeatCount(Integer.MAX_VALUE);
        anim.setInterpolator(new LinearInterpolator());
        anim.setStartDelay(150);
        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            public void onAnimationUpdate(ValueAnimator animation) {
                if(progress == 0) return ;
                animProgress = (int) animation.getAnimatedValue();
                post(invalidateRun);
            }
        });
        anim.start();
    }
    /** 控制动画on off */
    public void controlAnim(boolean start){
        if(start) startAnim();
        else cancelAnim();
    }
    private boolean isAnimOn(){return anim != null;}
    private void resetAnimAndStart(){
        cancelAnim();
        startAnim();
    }


    
    private float max = 100;
    private float progress = 0;
    public void setMax(int max) {
        this.max = max;
        if(!isAnimOn() && progress > 0) post(invalidateRun);
    }
    private Runnable invalidateRun = new Runnable() {
        public void run() { invalidate(); }
    };
    /** 设置进度 */
    public void setProgress(int progress) {
        if(this.progress == progress) return ;
        this.progress = progress;
        if(this.progress<0) this.progress = 0;
        if(this.progress>max) this.progress = max;
        if(!isAnimOn()) post(invalidateRun);
    }
    private float getProgressDecimal(){
        return progress/max;
    }
    private int color = Color.BLUE;
    private int borderWidth = 8;
    private int bgColor = Color.WHITE;
    /** 设置背景颜色 */
    public void setBgColor(int bgColor) {
        if(this.bgColor == bgColor) return ;
        this.bgColor = bgColor;
        if(!isAnimOn()) post(invalidateRun);
    }
    /** 设置进度颜色 */
    public void setColor(int color) {
        if(this.color == color) return ;
        this.color = color;
        paint.setColor(color);
        if(!isAnimOn()) post(invalidateRun);
    }
    /** 边框厚度 */
    public void setBorderWidth(int borderWidth) {
        if(this.borderWidth == borderWidth) return;
        this.borderWidth = borderWidth;
        if(!isAnimOn()) post(invalidateRun);
    }
    private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);{
        paint.setDither(true);
    }
    private Rect progressRect = new Rect();
    private final int animRectWidth = dp2px(30);
    private final RectF animRect = new RectF();
    @Override
    protected void onDraw(Canvas canvas) {
        /* 进度 */
        paint.setColor(color);
        paint.setStyle(Paint.Style.FILL);
        int progressWidth = (int) (getWidth()*getProgressDecimal());
        progressRect.set(0,0, progressWidth,getHeight());
        canvas.drawRect(progressRect,paint);
        /* 动画 */
        paint.setColor(Color.argb(90,255,255,255));
        animRect.set(animProgress-animRectWidth, 0, animProgress, getHeight());
        canvas.drawRoundRect(animRect,8,8,paint);
        /* 背景 */
        paint.setColor(bgColor);
        canvas.drawRect(progressWidth,0,getWidth(),getHeight(),paint);
        /* 边框 */
        paint.setColor(color);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(borderWidth);
        canvas.drawRect(0,0,getWidth(),getHeight(),paint);
    }

    private int dp2px(int dp){
        return (int) (getResources().getDisplayMetrics().density*dp+0.5);
    }
}
